var express = require('express');
var router = express.Router();
const { uploadFile } = require("../src/utils/upload")
const moviesService = require('../src/services/moviesServices')


//添加
router.post("/add", async (req, resp) => {
  //上传文件
  let resImg = await uploadFile(req, { fileType: "images" });
  console.log(resImg);
  let poster = resImg.data.url.split('/')[2]
  console.log(poster);
  let obj = {};
  let { cname,
    ename,
    types,
    area,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images } = resImg.data;
  obj = {
    cname,
    ename,
    types,
    area,
    poster,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images
  }
  let res = moviesService.showMovies({
    cname,
    ename,
    types,
    area,
    poster,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images
  });
  if (res != null) {
    resp.json({ "code": 1, "msg": "添加成功" })
  } else {
    resp.json({ "code": 0, "msg": "添加失败" })
  }
})

//查询所有电影
router.get("/queryall", async (req, resp) => {
  // let {studentid}= req.params;
  let res = await moviesService.queryAll()
  if (res != null) {
    resp.json({ "code": 0, "msg": "查询成功", 'count': 1, "data": res })
  } else {
    resp.json({ "code": 0, "msg": "查询失败" })
  }
})

//集合
// router.post("/collection", async (req, resp) => {
//   // let {studentid}= req.params;
//   let resImg = await uploadFile(req, { fileType: "img" });
//   console.log(resImg);
//   let img = resImg.data.url.split('/')[2]
//   console.log(res)
//   resp.json({ "code": 0, "msg": "上传成功", data: res })


// })


//查询ID
router.get("/queryonly/:_id", async (req, resp) => {
  let { _id } = req.params;
  let res = await moviesService.queryOnly(_id)
  if (res != null) {
    resp.json({ "code": 1, "msg": "查询成功", 'count': 1, "data": res })
  } else {
    resp.json({ "code": 0, "msg": "查询失败" })
  }
})

//查询类型
router.get("/querytype/:types/:lie", async (req, resp) => {
  let { types, lie } = req.params;
  let str = `{"${types}":"${lie}"}`
  let obj = JSON.parse(str)
  console.log(obj)
  let res = await moviesService.queryType(obj);
  // console.log(res);
  if (res != null) {
    resp.json({ "code": 0, "msg": "查询成功", "data": res })
  } else {
    resp.json({ "code": 1, "msg": "查询失败" })
  }

})

//查询首映地区
router.get("/queryarea/:area", async (req, resp) => {
  let { area } = req.params;
  console.log(area);
  let res = await moviesService.queryArea({ area });
  console.log(res);
  if (res != null) {
    resp.json({ "code": 1, "msg": "查询成功", "data": res })
  } else {
    resp.json({ "code": 0, "msg": "查询失败" })
  }

})
//删除
router.delete("/deletemov/:cname", async (req, resp) => {
  let { cname } = req.params;
  let res = await moviesService.deleteMov({ cname })
  console.log(res)
  if (res.deletedCount == '1') {
    resp.json({ "code": 1, "msg": "删除成功" })
  } else {
    resp.json({ "code": 0, "msg": "删除失败" })
  }
})

//修改电影信息
router.post("/modifiedmoives", async (req, resp) => {
  //上传文件
  let resImg = await uploadFile(req, { fileType: "images" });
  console.log(resImg);
  let poster = resImg.data.url.split('/')[2]
  console.log(poster);
  let obj = {};

  //获得参数
  let { _id,
    cname,
    ename,
    types,
    area,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images } = resImg.data;
  obj = {
    cname,
    ename,
    types,
    area,
    poster,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images
  }
  let res = await moviesService.modifiedMoives({
    _id,
    cname,
    ename,
    types,
    area,
    poster,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor,
    images
  });
  console.log(res);
  if (res.modifiedCount == 1) {
    resp.json({ "code": 1, "msg": "修改成功", "data": res })
  } else {
    resp.json({ "code": 0, "msg": "修改失败" })
  }
})


module.exports = router;
